﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Description;
using WcfFlashRemoting.ServiceModel.Dispatcher;

namespace WcfFlashRemoting.ServiceModel.Description
{
    class AmfEnablingBehavior : WebHttpBehavior
    {
        #region Client-side functionality

        protected override System.ServiceModel.Dispatcher.IClientMessageFormatter GetReplyClientFormatter(OperationDescription operationDescription, ServiceEndpoint endpoint)
        {
            return new AmfClientFormatter(operationDescription);
        }

        protected override System.ServiceModel.Dispatcher.IClientMessageFormatter GetRequestClientFormatter(OperationDescription operationDescription, ServiceEndpoint endpoint)
        {
            return new AmfClientFormatter(operationDescription);
        }

        #endregion

        #region Server-side functionality

        public override void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
        {
            endpointDispatcher.DispatchRuntime.Operations.Add(new System.ServiceModel.Dispatcher.DispatchOperation(endpointDispatcher.DispatchRuntime, "**AmfMultiple", "**AmfMultiple", null)
            {
                Invoker = new AmfMultipleInvoker(endpointDispatcher.DispatchRuntime),
                Formatter = new AmfMultipleOperationFormatter(),
            });

            endpointDispatcher.DispatchRuntime.Operations.Add(new System.ServiceModel.Dispatcher.DispatchOperation(endpointDispatcher.DispatchRuntime, "**AmfPing", "**AmfPing", null)
            {
                Invoker = new AmfPingInvoker(),
                Formatter = new AmfPingOperationFormatter(),
            });

            base.ApplyDispatchBehavior(endpoint, endpointDispatcher);
        }

        protected override System.ServiceModel.Dispatcher.WebHttpDispatchOperationSelector GetOperationSelector(ServiceEndpoint endpoint)
        {
            return new Dispatcher.AmfDispatchOperationSelector();
        }

        protected override System.ServiceModel.Dispatcher.IDispatchMessageFormatter GetReplyDispatchFormatter(OperationDescription operationDescription, ServiceEndpoint endpoint)
        {
            return new AmfDispatchFormatter(operationDescription);
        }

        protected override System.ServiceModel.Dispatcher.IDispatchMessageFormatter GetRequestDispatchFormatter(OperationDescription operationDescription, ServiceEndpoint endpoint)
        {
            return new AmfDispatchFormatter(operationDescription);
        }

        #endregion

        public override void Validate(ServiceEndpoint endpoint)
        {
            // intentionally skip validation
        }
    }
}
